package com.hqj.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqj.client.entity.ClientUser;
import com.hqj.client.mapper.ClientUserMapper;
import com.hqj.client.service.ClientUserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author 王建林
 * @version 1.0
 * @date 2020/10/14
 */
@Service
public class ClientUserServiceImpl implements ClientUserService {
    @Resource
    private ClientUserMapper ClientuserMapper;
    //获取配置文件里面的emails2695818931@qq.com
    @Value("${spring.mail.username}")
    private String emails;
    @Resource
    JavaMailSenderImpl javaMailSender;

    /**
     * 登陆判断
     *
     * @param username
     * @param password
     * @return com.hqj.client.entity.ClientUser
     * @date 2020/10/14 16:51
     */
    @Override
    public ClientUser login(String username, String password) {
        QueryWrapper<ClientUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username).eq("password", password);
        return ClientuserMapper.selectOne(wrapper);
    }

    /**
     * 邮件发送
     *
     * @param email
     * @return java.lang.String
     * @date 2020/10/14 17:02
     */
    @Override
    public String emailSet(String email) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //主题
        mailMessage.setSubject("欢迎登录南风博客");
        //生产验证码
        String cod = UUID.randomUUID().toString().substring(0, 4);
        //发送内容测试为验证码
        mailMessage.setText("验证码" + cod);
        //注册的邮箱
        mailMessage.setTo(email);
        //发件人邮箱
        mailMessage.setFrom(emails);
        javaMailSender.send(mailMessage);

        return cod;
    }

    /**
     * 注册
     * @param username
     * @param password
     * @param email
     * @param code
     * @return int
     * @date 2020/10/14 22:31
     */
    @Override
    public int register(String username, String password,
                        String email, String code) {
        String salt=UUID.randomUUID().toString();
        SimpleHash sh=new SimpleHash(
                "MD5",//algorithmName算法名称
                password, //source要加密的密码
                salt, //加密盐
                1);//加密次数
        String pwd=sh.toHex();
        ClientUser clientUser = new ClientUser();
        clientUser.setUsername(username);
        clientUser.setPassword(pwd);
        clientUser.setEmail(email);
        clientUser.setSalt(salt);
        return ClientuserMapper.insert(clientUser);
    }
    /**
     * 修改密码
     * @date 2020/10/16 14:41
     * @param username
     * @param password
     * @return com.hqj.client.entity.ClientUser
     */
    @Override
    public int changePassword(String username, String password,String newCode) {
        QueryWrapper<ClientUser>wrapper=new QueryWrapper<>();
        wrapper.eq("username",username).eq("password",password);
        ClientUser clientUser = ClientuserMapper.selectOne(wrapper);
        clientUser.setPassword(newCode);
        return ClientuserMapper.updateById(clientUser);
    }
    /**
     * 忘记密码
     * @date 2020/10/16 15:48
     * @param username	用户名
     * @param newCode	新密码
     * @param authCode	验证码
     * @return int
     */
    @Override
    public int forgetPassword(String username, String newCode, String authCode) {
        QueryWrapper<ClientUser>wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        ClientUser clientUser = ClientuserMapper.selectOne(wrapper);
        clientUser.setPassword(newCode);
        return ClientuserMapper.updateById(clientUser);
    }
}
